var display_style = isPhoneScreen()?"block":"flex";
$(function() {
	focusById();
	
	//$(".teacher_block").css("display",display_style);
	$(".teacher_block").css("display","none");
	$(".student_block").css("display","none");
	$(".vol_block").css("display","none");
	
	$("#enrollmentYear").click(function(){
		var a = $("#enrollmentYear").find("option").length;
		if(a && a==1){
			showErrMsg("enrollmentYear","请先选择学历");
		}
	});
	
	setTimeout(function(){
	$("#school").select2({
		ajax:{
			type: 'POST',
			url: rootPath + '/solr/schoolSearch',
			delay: 250,
			data: function (params) {
		          return {
		            text: params.term, // search term 请求参数
		            page: params.page
		          };
		        },
	        processResults: function (data, params) {
	            params.page = params.page || 1;
	            var srcList = data.returnCode=="0000"?data.returnObject:[];
	            var desList = [];
	            var temp;
	            for(var i=0;i<srcList.length;i++){
	            	temp = {};
	            	temp.id=srcList[i].id;
	            	temp.text=srcList[i].title[0];
	            	desList[i]=temp;
	            }
	            
	            return {
	              results: desList,//itemList
	              pagination: {
	                more: (params.page * 2) < data.total_count
	              }
	            };
	          },
	          cache: true
	        },
	        placeholder:'请选择学校',//默认文字提示
	        language: "zh-CN",
	        tags: false,//允许手动添加
	        allowClear: true,//允许清空
	        escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
	        minimumInputLength: 2,
	        formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
	        formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
	},50);
	
});

function showErrMsg(id,msg){
	$("#" + id + "Tip").text(msg);
	$("#" + id + "Msg").show();
}

function hideMsg(id) {
	$("#" + id + "Msg").hide();
}

function focusById(){
	
	$("#cpYear").focus(function(){
		hideMsg("cpYear");
	});
	$("#cpRleId").focus(function(){
		hideMsg("cpRleId");
	});
	$("#phone").focus(function(){
		hideMsg("phone");
	});
	$("#email").focus(function(){
		hideMsg("email");
	});
	$("#company").focus(function(){
		hideMsg("company");
	});
}

function getEnrollmentYear(){
	$("#enrollmentYear  option:gt(0)").remove();
	$("#enrollmentYear option:first").prop('selected', 'selected');
	hideMsg("enrollmentYear");
	var education = $("#education").val();
	var currentYear = parseInt(new Date().getFullYear());
	var option = "";
	switch(education){
	case"P":	//小学
		option = "";
		for(var i=currentYear-6;i<=currentYear;i++){
			option += "<option value=\""+i+"\">"+ i +"</option>"
		} 
		$(option).appendTo($("#enrollmentYear"));
		break;
	case "J":   //初中
	case "H":	//高中
		option = "";
		for (var i = currentYear - 3; i <= currentYear; i++) {
			option += "<option value=\""+i+"\">"+ i +"</option>"
		}
		$(option).appendTo($("#enrollmentYear"));
		break;
	}
}
function changeRole(e){
	$(e).removeAttr("style","color");
	var sortVal = $("#role").val();
	hideMsg("role");
	$("#sort").val(sortVal);
	$(".city").css("cursor", "pointer");
	switch(sortVal){
		case"7":  //学生
			$(".teacher_block").css("display",display_style);
			$(".student_block").css("display",display_style);
			$(".vol_block").css("display","none");
			$(".star").css("display","none");
			break;
		case"8":  //出行志愿者
		case"9":  //评审志愿者
			$(".teacher_block").css("display","none");
			$(".student_block").css("display","none");
			$(".vol_block").css("display",display_style);
			$(".star").css("display",'');
			break;
		  
		case"6": //教师
			$(".teacher_block").css("display",display_style);
			$(".student_block").css("display","none");
			$(".vol_block").css("display","none");
			$(".star").css("display",'');
			break;
		default:
	}
	hideMsg("enrollmentYear");
}